package com.CMC.yhdeng;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.JsonReader;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class HistoryActivity extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
	
		
		
	}

	@Override
	protected void onStart() {
		
		super.onStart();
		new AsyncTask<String, Void, List>(){

			@Override
			protected List doInBackground(String... params) {
				try {
					return getScoreList("john");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return null;
			}

			@Override
			protected void onPostExecute(List result) {
				List<ScoreData> lst=(List<ScoreData>)result;
//				for (ScoreData sd : lst) {
//					sd.print2Log();
//				}
				
				ScoreDataAdapter sda=new ScoreDataAdapter(HistoryActivity.this, 
						0, lst);
				
				ListView lv=(ListView) HistoryActivity.this.findViewById(R.id.lvHistory);
				lv.setAdapter(sda);
				lv.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent,
							View view, int position, long id) {
						
						
					}
					
				});
			}
			
			
		}.execute("");
	}

	static public HttpURLConnection setupConnection(String myurl) throws IOException
	{
//		InputStream is = null;
//		try {
			URL url = new URL(myurl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(10000 /* milliseconds */);
			conn.setConnectTimeout(15000 /* milliseconds */);
			conn.setRequestMethod("GET");
			conn.setDoInput(true);
			// Starts the query
			conn.connect();
//			int response = conn.getResponseCode();
//			Log.d("debug", "The response is: " + response);
//			is = conn.getInputStream();
//
//			List<BookContent> lst= readJsonStream(is);
//			conn.disconnect();
			return conn;

//		} finally {
//			// Makes sure that the InputStream is closed after the app is
//			// finished using it.
//			if (is != null) {
//				is.close();
//			}
//		}
	}
	static public List readScoreHistory(InputStream is) throws IOException
	{
		JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
	 		reader.beginArray();
	 		ArrayList<ScoreData> lst=new ArrayList<ScoreData>();
	 		while (reader.hasNext()) {// read a book
	 			
	 			ScoreData score=new ScoreData();
	 			score.readData(reader);
	 			lst.add(score);
	 			
	 		}
			reader.endArray();
			
//			reader.close();
			return lst;
	}
	static public List getScoreList(String username) throws IOException
	{
		String stringUrl = "http://www.urjotter.com/apis/exam/getScore";
		String sUrl=String.format("%s/%s", stringUrl, username);
//		URL url=new URL(sUrl);
		HttpURLConnection conn=setupConnection(sUrl);
//		conn.setReadTimeout(10000 /* milliseconds */);
//		conn.setConnectTimeout(15000 /* milliseconds */);
//		conn.setRequestMethod("GET");
//		conn.setDoInput(true);
//		// Starts the query
//		conn.connect();
		int response = conn.getResponseCode();
		Log.d("debug", "The response is: " + response);
		InputStream is =new BufferedInputStream( conn.getInputStream());

		List<BookContent> lst= readScoreHistory(is);

		return lst;
	}

}
